﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Think.Lenovo.Com.Common;
using Think.Lenovo.Com.Models;
namespace Think.Lenovo.Com.DataAccess
{
    /// <summary>
    /// 用户级别(一个角色可分为N个等级)
    /// </summary>
    public class UserLevel
    {
        /// <summary>
        /// 获得id号在ids集合的所有级别,如果ids为空,那么反回所有级别
        /// </summary>
        /// <param name="ids">主键id集合</param>
        /// <returns></returns>
        public LinkedList<UserLevelModel> GetUserLevels(IEnumerable<int> ids)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            LinkedList<UserLevelModel> list = new LinkedList<UserLevelModel>();
            StringBuilder conditions = new StringBuilder();
            if (ids != null)
            {
                IEnumerator<int> ie = ids.GetEnumerator();
                while (ie.MoveNext())
                {
                    conditions.Append(ie.Current.ToString())
                          .Append(",");
                }
                if (conditions.Length > 0)
                {
                    conditions.Remove(conditions.Length - 1, 1);
                }
                #region //查询
                StringBuilder sql = new StringBuilder(512);
                sql.Append("Select * from tbl_userlevel ")
                   .Append(" where id in (")
                   .Append(conditions.ToString())
                   .Append(")");

                DataSet ds = MysqlHelper.Query(conn, sql.ToString());
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            UserLevelModel model = new UserLevelModel();
                            model.Id = Convert.ToInt32(row["Id"]);
                            model.LevelName = Convert.ToString(row["LevelName"]);
                            list.AddLast(model);
                        }
                    }
                }

                #endregion
            }

            return list;
        }
        /// <summary>
        /// 获得所有用户级别
        /// </summary>
        /// <returns>所有用户级别</returns>
        public LinkedList<UserLevelModel> GetAllUserLevels()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            LinkedList<UserLevelModel> list = new LinkedList<UserLevelModel>();
            StringBuilder condtions = new StringBuilder();

            #region //查询
            StringBuilder sql = new StringBuilder(50);
            sql.Append("Select * from tbl_userlevel ");
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        UserLevelModel model = new UserLevelModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.LevelName = Convert.ToString(row["LevelName"]);
                        list.AddLast(model);
                    }
                }
            }
            #endregion

            return list;
        }
    }
}
